<?php

namespace Models;

use Phalcon\Db\RawValue;
use Phalcon\Mvc\Model\Validator\Uniqueness as UniquenessValidator;
use Phalcon\Mvc\Model\Validator\Uniqueness as Uniqueness;
use Phalcon\Mvc\Model\Validator\Regex as RegexValidator;
class ActUser extends BaseModel
{

    public function initialize()
    {
        $this->hasMany("id", "Models\Act", "aid",array('alias' => 'act'));
        //$this->hasMany("id","Models\User","uid",array('alias'=>'User'));
    }
    public function beforeValidationOnCreate()
    {
        if (!isset($this->id) || !$this->id) {
            $this->id = new RawValue('default');
        }
        if (!isset($this->name) || !$this->name) {
            $this->name = new RawValue('default');
        }
        if (!isset($this->tel) || !$this->tel) {
            $this->tel = new RawValue('default');
        }
        if (!isset($this->aid) || !$this->tel) {
            $this->aid = new RawValue('default');
        }


    }

    public function beforeValidationOnUpdate()
    {
        if (!isset($this->id) || !$this->id) {
            $this->id = new RawValue('default');
        }
        if (!isset($this->name) || !$this->name) {
            $this->name = new RawValue('default');
        }
        if (!isset($this->tel) || !$this->tel) {
            $this->tel = new RawValue('default');
        }
        if (!isset($this->aid) || !$this->tel) {
            $this->aid = new RawValue('default');
        }
    }
    public function validation()
    {

//        $this->validate(new Uniqueness(array(
//            'field' => 'tel',
//            'message' => '该号码已经提交，请勿重复报名',
//        )));
        $this->validate(new RegexValidator(array(
            'field' => 'tel',
            'pattern' => '/^[1][358]\d{9}$/',
            'message' => '请输入手机格式',
        )));
        if ($this->validationHasFailed() == true) {
            return false;
        }
    }

    public function notSave()
    {
        //Obtain the flash service from the DI container
        $flash = $this->getDI()->getFlash();

        //Show validation messages
        foreach ($this->getMessages() as $message) {
            $flash->error($message);
        }
    }

}